import os
import cv2
from modules.deduplicator import ImageDeduplicator


class DeduplicatorTester:
    def __init__(self, directory_path):
        self.directory_path = directory_path
        self.deduplicator = ImageDeduplicator()

    def run_deduplication_test(self):
        """运行去重测试"""
        try:
            print(f"开始去重测试目录: {self.directory_path}")
            frames_with_info = self._load_frames_info()
            unique_results = self.deduplicator.deduplicate_frames(frames_with_info)
            
         
            for result in unique_results:
                print(f"去重结果: {result['frame_path']} - 质量: {result['quality']:.2f}")
            print(f"\n去重后数量: {len(unique_results)}")
            print("去重测试完成")
        except Exception as e:
            print(f"去重测试运行错误: {str(e)}")

    def _load_frames_info(self):
        """加载帧信息"""
        frames_with_info = []
        files = os.listdir(self.directory_path)
        image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp'))]
        
        for image_file in image_files:
            image_path = os.path.join(self.directory_path, image_file)
            frame = cv2.imread(image_path)
            
            if frame is None:
                print(f"无法读取图像: {image_path}")
                continue
            
            # 分析帧内容
            frame_info = self.deduplicator.analyze_frame_content(frame)
            frame_info['frame_path'] = image_path
            frames_with_info.append(frame_info)
        
        return frames_with_info

# 使用示例
if __name__ == "__main__":
    tester = DeduplicatorTester(r'D:\code\video-scan\frame\frames')
    tester.run_deduplication_test()